﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

using EHTWebsite.Code;

/// <summary>
/// Author: toanvt
/// CreatedDate: 13/12/2011
/// Description: Danh sách chuyên mục tin tức
/// </summary>
public partial class Administrator_AdminNewsCategorys : System.Web.UI.Page
{
    private Int32 danhMucTinTucID = 0;

    private Int32 pageIndex = 1;
    private Int32 pageSize = 20;
    private Int32 totalRecord;

    private DataTable dtDanhmuctintuc = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
       
        try
        {
            danhMucTinTucID = Int32.Parse(Request.QueryString["DanhMucTinTucID"].ToString());
        }
        catch (Exception ex)
        {
            danhMucTinTucID = 0;
        }

        try
        {
            pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
        }
        catch (Exception ex)
        {
            pageIndex = 1;
        }

        if (!Page.IsPostBack)
        {
            dtDanhmuctintuc = DanhMucTinTucProvider.GetDanhsachtintucForBackEnd().Tables[0];
            LoadDataDanhMucTinTucs(danhMucTinTucID);
            CreateTreeViewCategory();
            
                               
            //FillDataIntoDataList(categoryID);

        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Administrator/AdminNewsCategoryIU.aspx?DanhMucID=" + danhMucTinTucID);
    }

    /// <summary>
    /// Author: chiennv
    /// CreatedDate:  4/2/2012
    /// Description: Load dữ liệu danh mục
    /// </summary>
    private void LoadDataDanhMucTinTucs(Int32 danhMucTinTucID)
    {
        DataSet dsDanhMucTinTuc = DanhMucTinTucProvider.GetDanhMucTinTucsByNhomDanhMucIDwithPaging(danhMucTinTucID, pageIndex, pageSize, out totalRecord);
            this.rpDanhMucTinTuc.DataSource = dsDanhMucTinTuc;
            this.rpDanhMucTinTuc.DataBind();

            foreach (RepeaterItem item in rpDanhMucTinTuc.Items)
            {
                CheckBox ckPublish = (CheckBox)item.FindControl("ckPublish");

                //Hiện biểu tượng tương ứng với trạng thái Publish
                if (ckPublish.Checked == false)
                {
                    ((ImageButton)item.FindControl("btnPublish")).ImageUrl = "Templates/Images/btncheck.png";
                }
                else
                {
                    ((ImageButton)item.FindControl("btnPublish")).ImageUrl = "Templates/Images/unpublish.jpg";
                }
            }
        PaggingControl1.PageURL = "AdminNewsCategorys.aspx?DanhMucTinTucID=" + danhMucTinTucID.ToString();
        PaggingControl1.PageIndex = pageIndex;
        PaggingControl1.TotalRecord = totalRecord;
    }


    /// <summary>
    /// Author : chiennv
    /// CreateDate : 3/02/2012
    /// Description : Phương thức tạo TreeView
    /// <param name="source"></param>
    /// <param name="e"></param>
    private void CreateTreeViewCategory()
    {
        TreeNode level0 = new TreeNode("Danh mục tin tức", "0");
        CreateTreeViewCategorys(level0, 0);
        tvNhomTinTuc.Nodes.Add(level0);
        ExpandPathOfTreeNode(tvNhomTinTuc.SelectedNode);
    }

    /// <summary>
    /// Author : chiennv
    /// CreateDate : 3/02/2012
    /// Description : Phương thức đệ quy tạo TreeView
    /// </summary>
    private void CreateTreeViewCategorys(TreeNode treeNode, Int32 _ChuyenMucChaID)
    {
        DataRow[] dataRowsSubCateogrys = dtDanhmuctintuc.Select("ChuyenMucChaID = " + _ChuyenMucChaID.ToString());

        Int32 amountOfSubCategory = dataRowsSubCateogrys.Count();

        //Điểm dừng đệ quy
        if (amountOfSubCategory == 0)
            return;

        TreeNode tn;

        for (Int32 i = 0; i < amountOfSubCategory; ++i)
        {
            Int32 subCategoryID = Int32.Parse(dataRowsSubCateogrys[i]["DanhMucTinTucID"].ToString());
            tn = new TreeNode(dataRowsSubCateogrys[i]["TenDanhMucTinTucVN"].ToString(), subCategoryID.ToString());
            tn.CollapseAll();
            CreateTreeViewCategorys(tn, subCategoryID);
            treeNode.ChildNodes.Add(tn);

            if (subCategoryID == danhMucTinTucID)
            {
                tn.Select();
            }

            tn = null;
        }
    }

    /// <summary>
    /// Author : toanvt
    /// CreatedDate : 10/02/2012
    /// Description : Expand Path of TreeNode
    /// </summary>
    /// <param name="tn"></param>
    private void ExpandPathOfTreeNode(TreeNode tn)
    {
        TreeNode tmpTreeNode = tn;

        if (tn == null)
            return;

        do
        {
            tmpTreeNode.Expand();
            TreeNode tmp = tmpTreeNode;
            tmpTreeNode = null;
            tmpTreeNode = tmp.Parent;
        }
        while (tmpTreeNode != null);
    }

    /// <summary>
    /// Ham xóa danh muc tin tuc
    /// author: chiennv
    /// create: 6/2/2012
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void tvNhomTinTuc_SelectedNodeChanged(object sender, EventArgs e)
    {
        Response.Redirect("AdminNewsCategorys.aspx?DanhMucTinTucID=" + this.tvNhomTinTuc.SelectedNode.Value);
    }

    protected void rpDanhMucTinTuc_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        String cmdName = e.CommandName;
        Int32 dMucTinTucID = 0;

        try
        {
            dMucTinTucID = Int32.Parse(e.CommandArgument.ToString());
        }
        catch (Exception ex)
        {          
        }

        LoadDataDanhMucTinTucs(0);
        if (cmdName.ToLower() == "delete1")
        {
            DataSet theDanhmuctintuc = DanhMucTinTucProvider.GetDanhMucTinTucsByNhomDanhMucID(dMucTinTucID);
            DataSet theTinTuc = TinTucProvider.GetTintucbyDanhMucTinTucID(dMucTinTucID,pageIndex,pageSize, out totalRecord);
            if (theDanhmuctintuc.Tables[0].Rows.Count > 1)
            {

                ltrMessage.Text = "Bạn không thể xóa danh mục này vì có chứa danh mục con";                          
                return;
            }
            else if (theTinTuc.Tables[0].Rows.Count > 0)
            {
                ltrMessage.Text = "Bạn không thể xóa danh mục này vì có chứa tin tức"; 
                return;
            }
            else
            {
                DanhMucTinTucProvider.DeleteDanhMucTinTucByDanhMucID(dMucTinTucID);
                LoadDataDanhMucTinTucs(0);
                Response.Redirect("AdminNewsCategorys.aspx");
               
            }

           
        }
        else if (cmdName == "publish")
        {
            DanhMucTinTucProvider.ChangePublishBydanhMucTinTucID(dMucTinTucID);
            LoadDataDanhMucTinTucs(danhMucTinTucID);
        }

        else if (cmdName == "change_number_of_order")
        {
            try
            {
                Int32 numberOfOrder = Int32.Parse(((TextBox)e.Item.FindControl("txtNumberOfOrder")).Text.Trim());
                DanhMucTinTucProvider.UpdateNumberOfOrderByDanhMucTinTucID(dMucTinTucID, numberOfOrder);
            }
            catch (Exception ex)
            {

            }

            LoadDataDanhMucTinTucs(danhMucTinTucID);

        }
        else if (cmdName == "save_all")
        {
            foreach (RepeaterItem item in rpDanhMucTinTuc.Items)
            {

                Int32 numberOfOrder = Int32.Parse(((TextBox)item.FindControl("txtNumberOfOrder")).Text.Trim());
                dMucTinTucID = Int32.Parse(((Literal)item.FindControl("ltrDanhMucTinTucID")).Text.Trim());
                TinTucProvider.UpdateNumberOfOrderByTinTucID(dMucTinTucID, numberOfOrder);
            }

            LoadDataDanhMucTinTucs(danhMucTinTucID);
        }
    }
}
